package com.cheng.example.geektime.designpatterns.j57.writeventbusauthor;



import java.util.ArrayList;
import java.util.List;

public class UserController {

    private UserService userService =new UserService(); // 依赖注入
    private EventBus eventBus;

    private static final int DEFAULT_EVENTBUS_THREND_POOL_SIZE = 20;

    public UserController() {
        eventBus = new EventBus();// 同步阻塞模式
//        eventBus = new AsyncEventBus(Executors.newFixedThreadPool(DEFAULT_EVENTBUS_THREND_POOL_SIZE));
    }

    //一次性设置好，之后也不可能动态的修改
    public void setRegObservers(List<Object> observers) {

        for (Object observer : observers) {
            eventBus.register(observer);
        }

    }

    public String register(String telephone, String password) {
        String userId = userService.register(telephone, password);
        eventBus.post(new RegNotificationObserver().new Son());
        return userId;
    }


    public static void main(String[] args) {
        UserController userController = new UserController();

        List<Object> regObservers = new ArrayList<>();

        regObservers.add(new RegNotificationObserver());
//        regObservers.add(new RegPromotionObserver());
        userController.setRegObservers(regObservers);

        userController.register("17513137853", "123456");
    }
}
